package com.whenjun.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.ifootbook.util.Util;
import com.whenjun.entity.IscDoorRecord;
import com.whenjun.enums.KqDoorTypeEnum;
import com.whenjun.mapper.IscDoorRecordMapper;
import com.whenjun.service.IIscDoorRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author whenjun
 * @since 2024-02-27
 */
@Service
public class IscDoorRecordServiceImpl extends ServiceImpl<IscDoorRecordMapper, IscDoorRecord> implements IIscDoorRecordService {

    /**
     * 门禁事件入库(包含刷卡和人脸)
     * @param data
     */
    @Transactional
    public IscDoorRecord record(JSONObject data) {
        Feature[] features = {Feature.AllowISO8601DateFormat};
        JSONObject jsonObject = JSON.parseObject(data.toJSONString(), features);

        JSONObject subData = jsonObject.getJSONObject("data");
        String cardNo = subData.getString("ExtEventCardNo");
        Integer doorId = subData.getInteger("ExtEventDoorID");
        Integer inOut = subData.getInteger("ExtEventInOut");
        String personId = subData.getString("ExtEventPersonNo");
        Integer userType = subData.getInteger("UserType");

        LocalDateTime now = LocalDateTime.now();

        IscDoorRecord iscDoorRecord = JSONObject.parseObject(jsonObject.toJSONString(), IscDoorRecord.class);

        iscDoorRecord.setCardNo(cardNo);
        iscDoorRecord.setDoorId(doorId);
        iscDoorRecord.setInOut(inOut);
        iscDoorRecord.setPersonId(personId);
        iscDoorRecord.setUserType(userType);
        iscDoorRecord.setCreateTime(now);
        iscDoorRecord.setSource(data.toJSONString());

        this.save(iscDoorRecord);
        return iscDoorRecord;
//        System.out.println(JSON.toJSONString(iscDoorRecord));
    }


    @Transactional
    public IscDoorRecord recordIptFace(JSONObject data) {
        Feature[] features = {Feature.AllowISO8601DateFormat};
        JSONObject jsonObject = JSON.parseObject(data.toJSONString(), features);

        JSONObject subData = jsonObject.getJSONObject("data");

        JSONObject faceMatch = subData.getJSONObject("faceRecognitionResult").getJSONArray("faceMatch").getJSONObject(0);

        JSONObject resInfo = subData.getJSONArray("resInfo").getJSONObject(0);

        // 相机编号
        String indexCode = resInfo.getString("indexCode");
        String cn = resInfo.getString("cn");

        String personId = faceMatch.getString("faceInfoCode");

        LocalDateTime now = LocalDateTime.now();

        IscDoorRecord iscDoorRecord = JSONObject.parseObject(jsonObject.toJSONString(), IscDoorRecord.class);

        String id = subData.getString("eventId") == null ? Util.getUUID32() : subData.getString("eventId");

        iscDoorRecord.setId(id);

        iscDoorRecord.setPersonId(personId);
        iscDoorRecord.setCreateTime(now);
        iscDoorRecord.setSrcName(cn);
        iscDoorRecord.setSrcIndex(indexCode);
        iscDoorRecord.setSource(data.toJSONString());

        this.save(iscDoorRecord);
        return iscDoorRecord;
//        System.out.println(JSON.toJSONString(iscDoorRecord));
    }

    /*
     * @Author whenjun
     * @Description  判断门禁类型
     * @Date 21:50 2024/3/3
     * @Param [indexCode, groupCode]
     * @return java.lang.Boolean
     **/
    @Transactional
    public Boolean checkIndexCodeInGroupType(String indexCode, String groupCode){
        Integer count = this.getBaseMapper().countIndexCodeInGroupType(indexCode, groupCode);
        return count > 0 ? true : false;
    }


    // 查询对应门禁类型的最近一次inout=in的记录

    public static void main(String[] args) {
        String dataStr = " {\n" +
                "                                        \"data\":  {\n" +
                "                                                  \"ExtAccessChannel\":  0,\n" +
                "                                                  \"ExtDeviceNo\":  0,\n" +
                "                                                  \"ExtEventAlarmInID\":  0,\n" +
                "                                                  \"ExtEventAlarmOutID\":  0,\n" +
                "                                                  \"ExtEventCardNo\":  \"1498442158\",\n" +
                "                                                  \"ExtEventCaseID\":  0,\n" +
                "                                                  \"ExtEventCode\":  196893,\n" +
                "                                                  \"ExtEventCustomerNumInfo\":  {\n" +
                "                                                            \"AccessChannel\":  0,\n" +
                "                                                            \"EntryTimes\":  0,\n" +
                "                                                            \"ExitTimes\":  0,\n" +
                "                                                            \"TotalTimes\":  0\n" +
                "                                                  },\n" +
                "                                                  \"ExtEventDoorID\":  1,\n" +
                "                                                  \"ExtEventID\":  \"5696bf21dfd87532ea57168e841a9cb3\",\n" +
                "                                                  \"ExtEventIDCardPictureURL\":  \"\",\n" +
                "                                                  \"ExtEventIdentityCardInfo\":  {\n" +
                "                                                            \"Address\":  \"\",\n" +
                "                                                            \"Birth\":  \"\",\n" +
                "                                                            \"EndDate\":  \"\",\n" +
                "                                                            \"IdNum\":  \"\",\n" +
                "                                                            \"IssuingAuthority\":  \"\",\n" +
                "                                                            \"Name\":  \"\",\n" +
                "                                                            \"Nation\":  0,\n" +
                "                                                            \"Sex\":  0,\n" +
                "                                                            \"StartDate\":  \"\",\n" +
                "                                                            \"TermOfValidity\":  0\n" +
                "                                                  },\n" +
                "                                                  \"ExtEventInOut\":  1,\n" +
                "                                                  \"ExtEventLocalControllerID\":  0,\n" +
                "                                                  \"ExtEventMainDevID\":  1,\n" +
                "                                                  \"ExtEventPersonNo\":  \"ca868431b326497485d607f94e49d6ed\",\n" +
                "                                                  \"ExtEventPictureURL\":  \"\",\n" +
                "                                                  \"ExtEventReaderID\":  1,\n" +
                "                                                  \"ExtEventReaderKind\":  0,\n" +
                "                                                  \"ExtEventReportChannel\":  0,\n" +
                "                                                  \"ExtEventRoleID\":  0,\n" +
                "                                                  \"ExtEventSubDevID\":  0,\n" +
                "                                                  \"ExtEventSwipNum\":  0,\n" +
                "                                                  \"ExtEventType\":  0,\n" +
                "                                                  \"ExtEventVerifyID\":  0,\n" +
                "                                                  \"ExtEventWearMask\":  2,\n" +
                "                                                  \"ExtEventWhiteListNo\":  0,\n" +
                "                                                  \"ExtReceiveTime\":  \"1708960087467670\",\n" +
                "                                                  \"Seq\":  5872,\n" +
                "                                                  \"UserType\":  1,\n" +
                "                                                  \"svrIndexCode\":  \"\"\n" +
                "                                        },\n" +
                "                                        \"eventId\":  \"5696bf21dfd87532ea57168e841a9cb3\",\n" +
                "                                        \"eventType\":  196893,\n" +
                "                                        \"happenTime\":  \"2024-02-26T23:08:06.000+08:00\",\n" +
                "                                        \"srcIndex\":  \"267a7292623d4a5299f2311af49a4d7d\",\n" +
                "                                        \"srcName\":  \"\",\n" +
                "                                        \"srcParentIndex\":  \"4130d87315b94a83b5eb6b5671b2feeb\",\n" +
                "                                        \"srcType\":  \"door\",\n" +
                "                                        \"status\":  0,\n" +
                "                                        \"timeout\":  0\n" +
                "                              }";

        JSONObject data = JSON.parseObject(dataStr);

//        record(data);
    }
}
